#!/bin/bash

params=$@


if [ $# -eq 0 ] ; then
  echo "$0: Missing params" ; 
  echo "Usage: $0 [all=r1.0.0] ..." ;
  exit 1 ;
else
  for p in $params ; do
    set -- `echo $params | tr '=' ' '`
    if [ -z "$2" ] ; then
      echo "merge branch is empty" 
      exit 1 ;
    fi ;
  done ;
fi ;

fullpath=`pwd -P .`
cd $fullpath


echo "--------------------------------------------------"
echo "Ready to git merge branch: $params"
echo "--------------------------------------------------"

for p in $params ; do
  set -- `echo $p | tr '=' ' '` 
  k=$1 
  v=$2 
  if [ $k == "all" ] ; then
    for f in */ ; do
      if [ -d "$f" ] ; then
      	echo "------------------------< $f >"
        cd $f
        branch=`git rev-parse --abbrev-ref HEAD`
        if [ "$branch" != "master" ] ; then
          git merge $v ;
        fi ;
        cd $fullpath ;
      fi ;
    done ;
  elif [ -d "$k" ] ; then
    cd $k 
    git merge $v
    cd $fullpath ;
  else
    echo "$k directory not exists!"
    exit 1 ;
  fi ; 
done ;
    







